#include "linuxheader.h"
#include <workflow/WFServer.h>
#include <workflow/Workflow.h>
#include <workflow/WFHttpServer.h>
static WFFacilities::WaitGroup waitGrop(1);
void sigHander(int num)
{
 waitGrop.done();
    fprintf(stderr,"grop is done\n");
}struct SeriesContext
{
    WFHttpTask *serverTask;
    int fd;
    char *buf;
    size_t filesize;
};
void IoCallBack(WFFileIOTask * ioTask)
{
        SeriesContext *context = static_cast<SeriesContext *>(series_of(ioTask)->get_context());
      auto resp2client = context->serverTask->get_resp();
      resp2client->add_header_pair("Content_Type,","text/html");
      resp2client->append_output_body(context->buf,context->filesize);
}

void process(WFHttpTask*serverTask)
{
   //创建一个io任务；//文件的信息还有其他的信息
   size_t filesize = 614;
   int fd = open("postfrom.html",O_RDONLY);
   char *buf = new char[filesize];
    WFFileIOTask* ioTask = WFTaskFactory::create_pread_task(fd,buf,0,0,IoCallBack);
    series_of(serverTask)->push_back(ioTask);
   SeriesContext * contesxt = new SeriesContext;
   contesxt->buf = buf;
   contesxt->fd = fd;
   contesxt->filesize = filesize;
   contesxt ->serverTask =serverTask;
    series_of(serverTask)->set_context(contesxt);
    series_of(serverTask)->set_callback([](const SeriesWork*series){
         fprintf(stderr,"series callback\n");
        fprintf(stderr,"series callBack  begin\n");
        SeriesContext *context = static_cast<SeriesContext *>(series->get_context());
        close(context->fd);
        delete[] context->buf;
        delete context;
       
    });
}
int main()
{
 signal(SIGINT,sigHander);
 WFHttpServer server(process);
if(server.start(1234)==0)
{
    waitGrop.wait();
    server.stop();
}
else
{
    fprintf(stderr,"server start failed\n");
    return -1;
}
return 0;
}





